//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
// 

#include "traceList.h"

void traceList::readAndStoreFile(std::string filePath){
    std::string trace;
    std::string list[4];
    int listint[4];
    ifstream traceFile;
    traceFile.open(filePath.c_str(), std::ifstream::in);
    if (!traceFile) {
        EV << "ERROR: file not found" << endl;
        return ;
    }
    while (!traceFile.eof()) {
        std::getline(traceFile, trace);
        std::istringstream iss(trace);
        iss >> list[0] >> list[1] >> list[2] >> list[3];
        list[0] = list[0].substr(0, list[0].length());
        listint[0] = atoi(list[0].c_str());
        list[1] = list[1].substr(0, list[1].length() - 1);
        listint[1] = atoi(list[1].c_str());
        list[2] = list[2].substr(0, list[2].length() - 1);
        listint[2] = atoi(list[2].c_str());
        time.push_back(listint[0]);
        sender.push_back(listint[1]);
        receiver.push_back(listint[2]);
    }
    traceFile.close();
}

/*
 * return the first time that is >= time t
 */
int traceList::indexForTime(double t){
    for(unsigned int i=0; i<time.size(); i++){
        if(time[i] >= t)
            return i;
    }
    return -1;
}

int traceList::senderAt(int i){
    return sender[i];
}

int traceList::receiverAt(int i){
    return receiver[i];
}

int traceList::firstTimestamp(){
    return time[0];
}

int traceList::lastTimestamp(){
    return time[time.size()-1];
}

int traceList::numberTraces(){
    return time.size();
}
